﻿#include <GetMessageInterface.h>

extern QMap<int,QVariantMap> expLogInfo;
extern QString currentStep;
extern speechModuleThread *spmoduleth;
extern QString expName;
extern QString expConsumablesType;
extern double totalExpTime;
extern QVector<double> cpsPartData;


/**
 * @brief 构造函数
 */
GetMessageInterface::GetMessageInterface()
{
}

/**
 * @brief 析构函数
 */
GetMessageInterface::~GetMessageInterface()
{}

//获取实验日志
Q_INVOKABLE QVariantList GetMessageInterface:: getExperimentLog(){
    QVariantList result;
    for (const auto &key : expLogInfo.keys())
    {
        result.append(expLogInfo.value(key));
    }
    return result;
}

Q_INVOKABLE QVariantList GetMessageInterface::getExperimentLog(QString type,QString name){
    QVariantList result;
    currentStep="0";
    expConsumablesType=type;
    expName=name;
    QString code=common->getItemCode("3",type);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select ID From exe_config where value_1='%1' and value_2='%2' and type=0").arg(name,code);
    query.exec(sql);
    QString id;
    while (query.next()) {
        id=query.value("ID").toString();
    }

    sql=QString("Select * From exe_config Where id='%1' and type<>'0' order by step").arg(id);
    query.exec(sql);
    expLogInfo.clear();

    while (query.next())
    {
        QVariantMap map;
        map["FinishTime"]="";
        map["Step"]=query.value("Step").toString();
        QString actionInfo;
        if(query.value("Type").toString()=="1")
        {
            actionInfo="注射器"+query.value("Value_1").toString()+query.value("Value_4").toString();
        }
        else if(query.value("Type").toString()=="2")
        {
            if(query.value("Value_1").toString()=="开启"){
                actionInfo+="打开阀门";
                QList<QString> openList=query.value("Value_2").toString().split(",");
                for(int i=0;i<openList.length();i++){
                    actionInfo+=openList[i]+",";
                }
            }
            if(query.value("Value_1").toString()=="关闭")
            {
                actionInfo+="关闭阀门";
                QList<QString> closeList=query.value("Value_2").toString().split(",");
                for(int i=0;i<closeList.length();i++){
                    actionInfo+=closeList[i]+",";
                }
            }
        }
        else if(query.value("Type").toString()=="3")
        {
            actionInfo="转盘转到"+query.value("value_1").toString()+"号位置";
        }
        else if(query.value("Type").toString()=="4")
        {
            actionInfo="检测";
        }
        else if(query.value("Type").toString()=="5"){
            actionInfo="延时"+query.value("Value_1").toString()+"秒";
        }
        else if(query.value("Type").toString()=="6"){
            actionInfo="循环执行步骤"+query.value("Value_1").toString()+"到"+"步骤"+query.value("Value_2").toString()+query.value("Value_3").toString()+"次";
        }
        else if(query.value("Type").toString()=="7"){
            int finalStep=query.value("step").toInt()+query.value("Value_1").toInt();
            actionInfo="跳到步骤"+QString::number(finalStep);
        }
        else if(query.value("Type").toString()=="8")
        {
            actionInfo="盖盖";
        }
        map["ActionInfo"]=actionInfo;
        map["Time"]="0";
        map["Status"]="未执行";
        expLogInfo.insert(query.value("Step").toInt(),map);
        result.append(map);
    }
    return result;
}

Q_INVOKABLE QString GetMessageInterface::getDefaultExperimentName(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select Value_1 From exe_config Where type='0' and isdef='1'";
    query.exec(sql);
    QString result;

    while (query.next())
    {
        result=query.value("Value_1").toString();
    }
    return  result;
}

Q_INVOKABLE QVariantMap GetMessageInterface::getOtherExperimentInfo(){
    QVariantMap map;
    if(expLogInfo.count()>0){
        map["StepInfo"]=currentStep+"/"+QString::number(expLogInfo.lastKey());
    }
    else{
        map["StepInfo"]=currentStep+"/"+QString::number(0);
    }
    map["ExpTime"]=QString::number(qRound(totalExpTime));

    return  map;
}

Q_INVOKABLE void GetMessageInterface::connectEqu(QString connectType, QString ip,QString port){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update IpInfo set Ip='%1',port='%2',IsDefault='1' Where ConnectType='%3'").arg(ip,port,connectType);
    query.exec(sql);
    sql=QString("Update IpInfo Set IsDefault='0' Where ConnectType<>'%1'").arg(connectType);
    query.exec(sql);
    if(connectType=="0")
    {
        udpClient= UdpClient::getInstance();
        udpClient->ClientConnectToHost(ip, port.toInt());
        QString sendInfo=CommCommand::I().connectToLc();
        udpClient->ClientSendingData(common->GetSendData(sendInfo));
    }
    if(connectType=="1")
    {
        tcpClient= TcpClient::getInstance();
        tcpClient->ClientConnectToHost(ip, port.toInt());
    }
    if(connectType=="2")
    {
        QMLSignal::I().emitCommunicationStatus(0);     // 发信号通知前端
    }
}

Q_INVOKABLE int GetMessageInterface::getDefaultConnectInfo()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select ConnectType From IpInfo Where IsDefault='1'";
    query.exec(sql);
    int type;
    while (query.next()) {
        type=query.value("ConnectType").toInt();
    }
    return type;
}

Q_INVOKABLE QVariantMap GetMessageInterface::getConnectInfo(int connectType){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    QVariantMap map;

    QString sql=QString("Select Ip,Port From IpInfo where ConnectType='%1'").arg(connectType);
    query.exec(sql);
    while (query.next()) {
        map["Ip"]=query.value("Ip").toString();
        map["Port"]=query.value("Port").toString();
    }

    return map;
}

Q_INVOKABLE void GetMessageInterface::setSpeechUseInfo(QString info){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update SpeechMode Set IsUse='%1'").arg(info);
    query.exec(sql);
}

Q_INVOKABLE void GetMessageInterface::connectSpeechMode(QString serialPort){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update SpeechMode Set SerialPort='%1'").arg(serialPort);
    query.exec(sql);
    spmoduleth->serialinfo();
}

Q_INVOKABLE int GetMessageInterface::getSpeechUseInfo(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select IsUse From SpeechMode";
    query.exec(sql);
    int result;
    while (query.next()) {
        result=query.value("IsUse").toInt();
    }
    return  result;

}

Q_INVOKABLE QStringList GetMessageInterface::getSerialPort(){
    QStringList result;
    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts()) {
        result.append(info.portName());
    }
    return result;
}

Q_INVOKABLE QVector<double> GetMessageInterface::getCPSData()
{
    QVector<double> result=cpsPartData;
    cpsPartData.clear();

    return result;
}

Q_INVOKABLE QStringList GetMessageInterface::getConsumablesName()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select Distinct value From Deftab Where Id=3";
    query.exec(sql);
    QStringList result;
    while (query.next()) {
        result.append(query.value("value").toString());
    }
    return  result;
}

Q_INVOKABLE QStringList GetMessageInterface::getExperimentNameByConsumables(QString consumables)
{
    QString code=common->getItemCode("3",consumables);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Distinct value_1 From exe_config Where type='0' and Value_2='%1'").arg(code);
    query.exec(sql);
    qDebug()<<sql;
    QStringList result;
    while (query.next()) {
        result.append(query.value("value_1").toString());
    }
    return  result;
}

Q_INVOKABLE QString GetMessageInterface::unitConversion(QString num,QString diameter,QString unit)
{
    if(unit=="mL/Min")
    {
        return  common->secondToMin(num,diameter);
    }
    else
    {
        return common->minToSecond(num,diameter);
    }
}

Q_INVOKABLE QString GetMessageInterface::unitConversionBySerial(QString num,QString type,QString serial,QString unit)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select InsideDiameter From SyringeMaintenance Where ConsumablesTypes='%1' and Id='%2'").arg(common->getItemCode("3",type),serial);
    QString diameter;
    query.exec(sql);
    while (query.next()) {
        diameter=query.value("InsideDiameter").toString();
    }
    return unitConversion(num,diameter,unit);
}

Q_INVOKABLE QStringList GetMessageInterface::getDefaultExeConfig()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value_1,value_2 From exe_config Where isdef='1' and type='0'";
    query.exec(sql);
    QStringList result;
    while (query.next())
    {
           result.append(common->getItemName("3",query.value("value_2").toString()));
           result.append(query.value("value_1").toString());
    }

    return result;
}
